import Vue from "vue";

import axios from 'axios';

axios.defaults.timeout = 5000;                        //响应时间
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';        //配置请求头
// axios.defaults.baseURL = 'http://vonym.cn/huobi-api/';
axios.defaults.baseURL = 'http://localhost:5257/huobi-api/';
//http request 拦截器
axios.interceptors.request.use(
	config => {
		config.data = JSON.stringify(config.data);
		if (localStorage.getItem('current_user')) {
			let AUTH_TOKEN = localStorage.getItem('current_user');
			config.headers = {
				'authorization': AUTH_TOKEN,
				'Content-Type': 'application/json; charset=utf-8'
			}
		}
		return config;
	}, error => {
		return Promise.reject(error);
	}
);
// 响应拦截器
axios.interceptors.response.use(
	res => {// 请求成功
		if (res.data.code === 700 || res.data.code === 701 || res.data.code === 720 || res.data.code === 800) {//请先登录
			Vue.prototype.router.push('/login')
		} else {
			return Promise.resolve(res);
		}
	}, error => {// 请求失败
		const {response} = error;
		if (response) {
			// 请求已发出，但是不在2xx的范围
			return Promise.reject(response);
		} else {
			return Promise.reject(error);
		}
	}
);

let api = {
	basicInfo: {
		currency: {
			queryPage(params) {
				return axios.get("currency/queryPage", {params});
			},
			queryInfo(params) {
				return axios.get("currency/queryInfo", {params});
			},
			edit(data) {
				return axios.post("currency/edit", data);
			}
		},
		symbolKline: {
			queryPage(params) {
				return axios.get("symbol/queryPage", {params});
			},
			queryInfo(params) {
				return axios.get("symbol/kline", {params});
			},
			edit(data) {
				return axios.post("symbol/edit", data);
			},
			queryAll() {
				return axios.get("symbol/queryAll");
			}
		}
	},
	job: {
		queryPage(params) {
			return axios.get("job/queryPage", {params});
		},
		query(params) {
			return axios.get("job/query", {params});
		},
		add(data) {
			return axios.post("job/add", data);
		},
		resume(data) {
			return axios.post("job/resume", data);
		},
		pause(data) {
			return axios.post("job/pause", data);
		},
		delete(data) {
			return axios.post("job/delete", data);
		}
	},
	order: {
		queryPage(params) {
			return axios.get("order/queryPage", {params});
		},
		queryInfo(params) {
			return axios.get("order/query", {params});
		},
		save(data) {
			return axios.post("order/save", data);
		},
		cancel(params) {
			return axios.get("order/cancel", {params});
		},
		delete(params) {
			return axios.get("order/delete", {params});
		}
	},
	user: {
		register(data) {
			return axios.post("register", data);
		},
		login(data) {
			return axios.post("login", data);
		},
		queryInfo() {
			return axios.get("user/queryInfo");
		},
		edit(data) {
			return axios.post("user/edit", data);
		},
		accountInfo() {
			return axios.get("account/query");
		}
	},
	wallet: {
		currentAmount() {
			return axios.get("account/currentAmount");
		},
		curve() {
			return axios.get("account/curve");
		}
	}
};

export default {
	install(Vue) {
		Vue.prototype.$axios = axios;
		Vue.prototype.api = api;
	}
}